<?php

namespace plugins\yongtai\model;


use plugins\yongtai\Utils;
use tree\Tree;

class ArticleCategoryModel extends BaseModel
{
    protected $table = "yt_z_article_category";

    function CategoryOptions()
    {
        return Utils::Map($this->GetCategoryList(), function ($item) {
            return ['content' => $item['html'] . $item['title'], 'value' => $item['id']];
        });
    }

    function GetCategoryList($display = 1)
    {
        if ($display == 0) {
            $where = [];
        } else {
            $where['display'] = $display;
        }
        $list = Utils::Map(Utils::Tree((new ArticleCategoryModel())->GetListAll($where)), function ($item) {
            $str = "";
            if ($item['level'] - 1 > 0) {
                $str = str_repeat("┄ ", $item['level'] - 1);
                $str = substr_replace($str, "└", strpos($str, "┄"), strlen("┄"));
            }
            $item['html'] = $str;
            return $item;
        });
        return $list;
    }

    function GetCategoryTree($select_id = 0, $pid = 0, $display = 1)
    {
        if ($display == 0) {
            $where = [];
        } else {
            $where['display'] = $display;
        }
        $list = $this->where($where)->select()->toArray();
        $list = Utils::ResetArrKeyList($list, ['parent_id'], function ($item) {
            return [$item['pid']];
        });
        $tree = new Tree();
        $tree->init($list);
        return $tree->getTree($pid, "<option value=\$id \$selected>\$spacer\$title</option>", $select_id);
    }
}
